package com.zyk.grate_offer.class02;

import java.util.HashMap;

/**
 * @author zhangsan
 * @date 2021/4/23 11:54
 */
public class Code07_SetAll {

    static class Pair {
        String val;
        int timestamp;

        public Pair(String val, int timestamp) {
            this.val = val;
            this.timestamp = timestamp;
        }
    }

    // set(k, v) , get(k)   setAll(v);
    static class Map {

        HashMap<String, Pair> data;
        int curTime;
        int setAllTime;
        String setAllVal;

        public Map() {
            data = new HashMap<>();
        }

        public void put(String key, String val) {
            data.put(key, new Pair(val, curTime++));
        }

        public String get(String key) {
            Pair pair = data.get(key);
            return pair.timestamp < setAllTime ? setAllVal : pair.val;
        }

        public void setAll(String val) {
            setAllTime = ++curTime;
            setAllVal = val;
        }

    }

    // for test
    public static void main(String[] args) {
        Map map = new Map();
        map.put("A", "我是A");
        map.put("B", "我是B");
        System.out.println(map.get("A"));
        System.out.println(map.get("B"));
        map.setAll("AAA");
        System.out.println("setAll(AAA)");
        System.out.println(map.get("A"));
        System.out.println(map.get("B"));
        map.put("C", "我是C");
        map.put("D", "我是D");
        map.put("E", "我是E");
        map.put("F", "我是F");
        map.setAll("PPPP");
        System.out.println(map.get("A"));
        System.out.println(map.get("B"));
        System.out.println(map.get("C"));
        System.out.println(map.get("D"));
        System.out.println(map.get("F"));

    }

}
